package com.sdy.resdir.provider;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.resdir.api.ResApi;
import com.sdy.mvc.annotation.RemoteService;
import com.sdy.resdir.api.param.UpdateApiResInfoParam;
import com.sdy.resdir.biz.mapper.RdParameterInformationMapper;
import com.sdy.resdir.biz.mapper.RdResourceDirMapper;
import com.sdy.resdir.biz.model.RdParameterInformation;
import com.sdy.resdir.biz.model.RdResDetailsApi;
import com.sdy.resdir.biz.model.RdResourceDir;
import com.sdy.resdir.biz.service.RdResDetailsApiService;
import com.sdy.resdir.biz.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * RPC调用
 *
 * @author hyh
 */
@Slf4j
@RemoteService
public class ResProvider implements ResApi {

    @Resource
    private RdResourceDirMapper rdResourceDirMapper;
    @Autowired
    private RdResDetailsApiService rdResDetailsApiService;
    @Resource
    private RdParameterInformationMapper paramMapper;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int saveOrUpdateApiRes(UpdateApiResInfoParam res) {
        RdResourceDir resInfo = CopyUtil.clone(res, RdResourceDir.class);
        resInfo.setResState(res.getInterfaceState());
        RdResDetailsApi apiInfo = CopyUtil.clone(res, RdResDetailsApi.class);
        List<RdParameterInformation> params = CopyUtil.batchClone(res.getParam(), RdParameterInformation.class);
        int resState = 0;
        boolean apiInfoState = false;
        int paramsState = 0;
        if (null != resInfo.getId()) {
            // 未上线资源申请上线时dcsb实施人员进行实施操作,放入dcsbId
            // 默认未发布  默认受限  资源类型为API接口类
            resInfo.setCollectionSituation(1).setResPower(2).setResType(1)
                    .setIsRelease(0).setModifyTime(new Date()).setResLevel(1).setIsFail(1);
            resState = rdResourceDirMapper.updateById(resInfo);
            // TODO 可能需要删除两个资源副表的信息重新插入，避免数据冗余
            apiInfo.setResDirId(resInfo.getId()).setCreateTime(new Date()).setInterfaceState(1);
            apiInfoState = rdResDetailsApiService.save(apiInfo);
        } else {
            // 根据resDcsbId查询该数据,如果该数据存在则执行更新,否则执行插入
            RdResourceDir rdResourceDir = rdResourceDirMapper.selectOne(Wrappers.<RdResourceDir>lambdaQuery().eq(RdResourceDir::getResDcsbId, resInfo.getResDcsbId()));
            if (null != rdResourceDir) {
                resInfo.setModifyTime(new Date());
                resState = rdResourceDirMapper.update(resInfo, Wrappers.<RdResourceDir>lambdaUpdate().eq(RdResourceDir::getResDcsbId, resInfo.getResDcsbId()));
                apiInfo.setModifyTime(new Date());
                // 如果没有查询到本来存在的API信息,则此处需要新增api信息
                RdResDetailsApi one = rdResDetailsApiService.getOne(Wrappers.<RdResDetailsApi>lambdaQuery().eq(RdResDetailsApi::getResDirId, rdResourceDir.getId()));
                if (null != one) {
                    apiInfoState = rdResDetailsApiService.update(
                            Wrappers.<RdResDetailsApi>lambdaUpdate().set(RdResDetailsApi::getInterfaceName, apiInfo.getInterfaceName())
                                    .set(RdResDetailsApi::getInterfaceType, apiInfo.getInterfaceType())
                                    .set(RdResDetailsApi::getRequestType, apiInfo.getRequestType())
                                    .set(RdResDetailsApi::getSupportFormat, apiInfo.getSupportFormat())
                                    .set(RdResDetailsApi::getApiDesc, apiInfo.getApiDesc())
                                    .set(RdResDetailsApi::getApiAddress, apiInfo.getApiAddress())
                                    .set(RdResDetailsApi::getRequestDemo, apiInfo.getRequestDemo())
                                    .set(RdResDetailsApi::getReturnDemo, apiInfo.getReturnDemo())
                                    .eq(RdResDetailsApi::getResDirId, rdResourceDir.getId()));
                } else {
                    // 没有查询到API信息则新增信息
                    rdResDetailsApiService.save(apiInfo.setResDirId(rdResourceDir.getId()).setCreateTime(new Date()).setInterfaceState(1));
                }
            } else {
                // 默认未发布  默认受限  资源类型为API接口类
                resInfo.setCollectionSituation(1).setResPower(2).setResType(1).setResCode(UUID.randomUUID().toString()).setModifyTime(new Date())
                        .setIsRelease(0).setCreateTime(new Date()).setModifyTime(new Date()).setResLevel(1).setIsFail(1).setResState(1);
                resState = rdResourceDirMapper.insert(resInfo);
                apiInfo.setResDirId(resInfo.getId()).setCreateTime(new Date()).setInterfaceState(1);
                apiInfoState = rdResDetailsApiService.save(apiInfo);
            }
        }
        paramMapper.delete(Wrappers.<RdParameterInformation>lambdaUpdate().eq(RdParameterInformation::getOwnResId, apiInfo.getId()));
        for (RdParameterInformation param : params) {
            param.setOwnResId(apiInfo.getId());
        }
        if (!params.isEmpty()) {
            paramsState = paramMapper.batchInsert(params);
        } else {
            paramsState = 1;
        }
        if (resState != 0 || !apiInfoState || paramsState != 0) {
            return 1;
        } else {
            return 0;
        }
    }

}
